/**
* ...
* @author Default
* @version 0.1
*/

package com.crlclr.managers {
	import com.crlclr.common.ILoader;
	import com.crlclr.events.LoaderEvent;
	
	import flash.events.*;

	public class LoaderManager extends EventDispatcher {
		
		private var loading:Boolean = false;
		private var loaderList:Array;
		private var loader:ILoader;
		private static var instance:LoaderManager = new LoaderManager ();
		public static function getInstance():LoaderManager {
			return instance;
		}
		
		public function LoaderManager() {
			if (instance != null) {
				throw new Error("���ܴ�������");
			}
			loaderList = new Array();
		}
		
		public function clean():void{
			this.loaderList.slice(0);
			try{
				this.loader.close();
			}catch(e:Error){
				
			}
			
			this.end();
		}
		
		
		public function addItem(item:ILoader,onlyone:Boolean=false,isPreLoader:Boolean = false):void {
			var index:int = this.indexOf(item);
			if ( index> -1) {
				this.loaderList.splice(index, 1);
			}
			if (onlyone) {
				try{
					this.loader.close();
				}catch (e:Error) {
				}
				this.loaderList = new Array();
				this.loaderList.push(item);
				this.loading = false;
			}else{
				if (isPreLoader) {
					this.loaderList.push(item);
				}else {
					loaderList.unshift(item);
				}
			}
			
			if (!loading) {
				this.start();
			}
		}
		
		public function start():Boolean {
			var event:LoaderEvent = new LoaderEvent(LoaderEvent.START);
			this.dispatchEvent(event);
			return loadNextItem();
		}
		public function enter(event:ProgressEvent):void {
			this.dispatchEvent(event);
		}
		public function end():void {
			var event:LoaderEvent = new LoaderEvent(LoaderEvent.END);
			this.dispatchEvent(event);
		}
		
		public function loadNextItem():Boolean {

			if (Length > 0) {
				loading = true;
				loader = loaderList.pop() as ILoader;
				bindEvent(loader);
				loader.load();
				return true;
			}else {
				loading = false;
				end();
				return loading;
			}
		}
		
		private function bindEvent(loader:ILoader):void {
			loader.addEventListener(Event.INIT, eventHander);
			loader.addEventListener(Event.COMPLETE, eventHander);
			loader.addEventListener(IOErrorEvent.IO_ERROR, eventHander);
			loader.addEventListener(ProgressEvent.PROGRESS,progressHander);
		}
		private function removeEvent(loader:ILoader):void {
			loader.removeEventListener(Event.INIT, eventHander);
			loader.removeEventListener(Event.COMPLETE, eventHander);
			loader.removeEventListener(IOErrorEvent.IO_ERROR, eventHander);
			loader.removeEventListener(ProgressEvent.PROGRESS,progressHander);
		}
		
		
		private function eventHander(event:Event):void {
			switch(event.type) {
				case Event.INIT :
				case Event.COMPLETE :
				case IOErrorEvent.IO_ERROR:
					removeEvent(event.target as ILoader);
					this.loadNextItem();
					break;
			}
		}
		private function progressHander(event:ProgressEvent):void {
			this.enter(event);
		}
		
		public function container(item:ILoader):Boolean {
			for each (var i:ILoader in loaderList) {
				if (i == item) {
					return true;
				}
			}
			return false;
		}
		public function indexOf(item:ILoader):int {
			var len:int = Length;
			for (var i:int = 0; i < len; i++ ) {
				if (loaderList[i] as ILoader == item) {
					return i;
				}
			}
			return -1;
		}		
		public function get Length():int {
			return loaderList.length;
		}
	}
	
}
